package tms.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.util.UUID;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Tools {
    public static final Font TAHOMA_BOLD_16 = new Font("Tahoma", Font.BOLD, 16);
    public static final Font TAHOMA_BOLD_14 = new Font("Tahoma", Font.BOLD, 14);
    public static final Font TAHOMA_BOLD_13 = new Font("Tahoma", Font.BOLD, 13);
    public static final Font TAHOMA_BOLD_11 = new Font("Tahoma", Font.BOLD, 11);
    public static final Font TAHOMA_BOLD_12 = new Font("Tahoma", Font.BOLD, 12);
    public static final Font TAHOMA_PLAIN_12 = new Font("Tahoma", Font.PLAIN, 12);
    public static final Font TAHOMA_BOLD_ITALIC_11 = new Font("Tahoma", Font.BOLD | Font.ITALIC, 11);

    public static final String FREQUENT_FLY_NUMBER_PREFIX = "FFN";
    public static final String FLIGHT_NUMBER_PREFIX = "FLN";
    public static final String ACCOUNT_NUMBER_PREFIX = "ACN";

    public static final String[] ALL_TITLES = new String[] {"Mr", "Mrs", "Miss"};
    public static final String[] ALL_NATIONS = new String[] {"Vietnam", "USA", "France"};

    public static final String DATE_FORMAT = "dd - MM - yyyy HH:mm";
    public static final String DATE_FORMAT_NO_TIME = "yyyy-MM-dd";
    public static final String DATE_FORMAT_NO_TIME_2 = "dd - MM - yyyy";

    public static JLabel createLabel(String caption) {
        return createLabel(caption, TAHOMA_PLAIN_12);
    }

    /**
     * Create a new label with a specified font.
     * @param label
     * @param font
     *      Get from Tools
     * @return
     */
    public static JLabel createLabel(String caption, Font font) {
        JLabel label = new JLabel(caption);
        label.setFont(font);
        return label;
    }

    public static JButton createButton(String caption) {
        JButton button = new JButton(caption);
        button.setFont(TAHOMA_PLAIN_12);
        return button;
    }

    /**
     * Create a new combo box with the default normal font.
     * The data or model should be set manually.
     * @see {@link DefaultComboBoxModel}
     */
    public static JComboBox createComboBox() {
        JComboBox cmBox = new JComboBox();
        cmBox.setFont(TAHOMA_PLAIN_12);
        return cmBox;
    }

    public static JPasswordField createPasswordField() {
        JPasswordField pField = new JPasswordField();
        pField.setFont(TAHOMA_PLAIN_12);
        return pField;
    }

    public static JTextField createTextField(String defaultText) {
        JTextField tField = new JTextField();
        tField.setText(defaultText);
        tField.setFont(TAHOMA_PLAIN_12);
        return tField;
    }

    /**
     * Create a new check box with unselected is default.
     * The selecting state should be set manually
     * @param caption
     * @return
     */
    public static JCheckBox createCheckBox(String caption) {
        JCheckBox cBox = new JCheckBox(caption);
        cBox.setFont(TAHOMA_PLAIN_12);
        return cBox;
    }

    public static JRadioButton createRadioButton(String caption) {
        JRadioButton rdb = new JRadioButton(caption);
        rdb.setFont(TAHOMA_PLAIN_12);
        return rdb;
    }

    /**
     * Add a label to Parent container.
     * @param parent parent container
     * @param labelText
     * @param font ex: Tools.TAHOMA_BOLD_16
     * @param x position x
     * @param y position y
     * @param width
     * @param height
     */
    public static JLabel addJLabel(JPanel parent, String labelText, Font font,
            int x, int y, int width, int height) {
        JLabel lbl = new JLabel(labelText);
        if (font != null) {
            lbl.setFont(font);
        }
        lbl.setBounds(x, y, width, height);
        parent.add(lbl);
        return lbl;
    }

    /**
     * Add a label to Parent container.
     * @param parent parent container
     * @param buttonText
     * @param font ex: Tools.TAHOMA_BOLD_16
     * @param x position x
     * @param y position y
     * @param width
     * @param height
     * @param action ActionListener of the button
     * @param textColor ex: Color.RED, BLACK if null
     */
    public static void addJButton(JPanel parent, String buttonText, Font font,
            int x, int y, int width, int height, ActionListener action, Color textColor) {
        JButton btn = new JButton(buttonText);
        if (font != null) {
            btn.setFont(font);
        }
        btn.setBounds(x, y, width, height);
        if (textColor != null) {
            btn.setForeground(textColor);
        } else {
            btn.setForeground(Color.BLACK);
        }

        if (action != null) {
            btn.addActionListener(action);
        }
        parent.add(btn);
    }

    public static JTextField addJTextField(JPanel parent, int x, int y,
            int width, int height, int columns) {
        JTextField textField = new JTextField();
        textField.setBounds(x, y, width, height);
        textField.setColumns(columns);
        parent.add(textField);
        return textField;
    }


    public static JCheckBox addJCheckBox(JPanel parent, int x, int y, int width,
            int height, boolean selected, String text, ActionListener action) {
        JCheckBox checkBox = new JCheckBox();
        checkBox.setBounds(x, y, width, height);
        checkBox.setSelected(selected);
        checkBox.setText(text);
        if (action != null) {
            checkBox.addActionListener(action);
        }
        parent.add(checkBox);
        return checkBox;
    }


    public static JComboBox addComboBox(JPanel parent, int x, int y,
            int width, int height, Object[] items) {
        JComboBox comboBox = new JComboBox();
        comboBox.setBounds(x, y, width, height);

        if (items != null) {
            for (Object o : items) {
                comboBox.addItem(o);
            }
        }
        parent.add(comboBox);
        return comboBox;
    }

    public static JPasswordField addJPasswordField(
            JPanel parent, int x, int y, int width, int height) {
        JPasswordField field = new JPasswordField();
        field.setBounds(x, y, width, height);
        parent.add(field);
        return field;
    }

//  public static String newFrequentFlightNumber() {
//      String res = "FFN";
//      res += UUID.randomUUID().toString().substring(0, 7);
//      return res.toUpperCase();
//  }

    /**
     * @param prefix Tools.FrequentFlyNumberPrefix || Tools.FlightNumberPrefix
     * @return new String with given prefix following by 7 random character
     */
    public static String generateString(String prefix) {
        String res = prefix;
        res += UUID.randomUUID().toString().substring(0, 7);
        return res.toUpperCase();
    }

    /**
     * Put a frame in a position that center the screen.
     * @param frame
     */
    public static void centerFrame(JFrame frame) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension size = frame.getSize();
        screenSize.height = screenSize.height / 2;
        screenSize.width = screenSize.width / 2;
        size.height = size.height / 2;
        size.width = size.width / 2;
        int y = screenSize.height - size.height;
        int x = screenSize.width - size.width;
        frame.setLocation(x, y);
    }

}
